package com.framework.spring.scan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 优先级落后于spring内置的ConfigurationClassPostProcessor
 * 那么当在TestBeanDefinitionRegistryPostProcessor注册beanDefinition时，就不能作为一个配置类，只能作为一个普通类
 * Y能正常实例化，但是里面的ComponentScan是无效的,@Bean 也会失效
 */
@Component
public class TestBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

	}

	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Y.class).getBeanDefinition();
		registry.registerBeanDefinition("y", beanDefinition);
	}
}
